package com.example.login.Controller;

import com.example.login.Mapper.*;
import com.example.login.Pojo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Controller
public class OrderController {
    @Autowired
    GoodsMap goodsMap;
    @Autowired
    OrderMap orderMap;
    @Autowired
    MobilePhoneMap mobilePhoneMap;
    @Autowired
    PCMap pcMap;
    @Autowired
    MP3Map mp3Map;
    Map map= new HashMap<>(1);
    ArrayList<mobileGoods> mobilePhoneList=new ArrayList<>(1);
    ArrayList<PCGoods> pcList=new ArrayList<>(1);
    ArrayList<mp3Goods> mp3List=new ArrayList<>(1);
    @RequestMapping("/user/order/{id}/{num}/{sku_code}")
    public String buy(@PathVariable("id") int id, @PathVariable("num") int num,
                      @PathVariable("sku_code") String sku,
                      Model model, HttpSession session)
    {   String cid=goodsMap.determine(id);
        mobilePhoneList.clear();
        mp3List.clear();
        pcList.clear();
        map.clear();
        Date date=new Date();
        int order_id=orderMap.getMaxId()+1;
        User user=(User)session.getAttribute("user");
        int user_id=user.getId();
        switch (cid){
            case "MobilePhone":
                mobilePhoneList.add(goodsMap.getMobileGood(id,sku));
                map.put(id+" "+sku,num);
                System.out.println(mobilePhoneList);
                System.out.println(map);
                orderMap.insert(order_id,mobilePhoneList.get(0).getId(),1,mobilePhoneMap.getPriceById(id,sku),
                        0,mobilePhoneMap.getPriceById(id,sku),date.toString()
                        ,0,user_id,num,mobilePhoneList.get(0).getSku_code());
                break;
            case "PC":
                pcList.add(goodsMap.getPCGood(id,sku));
                map.put(id+" "+sku,num);
                orderMap.insert(order_id,pcList.get(0).getId(),1,pcMap.getPriceById(id,sku),
                        0,pcMap.getPriceById(id,sku),date.toString()
                        ,0,user_id,num,pcList.get(0).getSku_code());
                break;
            case "MP3":
                mp3List.add(goodsMap.getMp3Good(id,sku));
                map.put(id+" "+sku,num);
                orderMap.insert(order_id,mp3List.get(0).getId(),1,mp3Map.getPriceById(id,sku),
                        0, mp3Map.getPriceById(id,sku),date.toString()
                        ,0,user_id,num,mp3List.get(0).getSku_code());
                break;
        }

        model.addAttribute("MobilePhoneList",mobilePhoneList);
        model.addAttribute("NumMap",map);
        model.addAttribute("PCList",pcList);
        model.addAttribute("MP3List",mp3List);
        //session传值
        session.setAttribute("mobile",mobilePhoneList);
        session.setAttribute("map",map);
        session.setAttribute("pc",pcList);
        session.setAttribute("mp3",mp3List);
        return "结算/确认订单2";
    }
    @RequestMapping("/user/order")
    public String DisplayOrder(Model model,HttpSession session) {
        User user =(User)session.getAttribute("user");
        int user_id = user.getId();
        ArrayList<Order> orders = orderMap.getOrder(user_id);
        Map map = new HashMap();
        for (int i = 0; i < orders.size(); i++) {
            int item_id = orders.get(i).getItem_id();
            String cid = goodsMap.determine(item_id);
            String sku = orders.get(i).getSku_code();
            switch (cid) {
                case "MobilePhone":
                    String mobileImage = mobilePhoneMap.getImage(item_id, sku);
                    map.put(item_id + sku, mobileImage);
                    break;
                case "PC":
                    String pcImage = pcMap.getImage(item_id, sku);
                    map.put(item_id + sku, pcImage);
                    break;
                case "MP3":
                    String mp3Image = mp3Map.getImage(item_id, sku);
                    map.put(item_id + sku, mp3Image);
                    break;
            }
        }
        System.out.println(orders);
        System.out.println(map);
        model.addAttribute("orderList", orders);
        model.addAttribute("ImageMap", map);
        return "结算/订单";
    }


}
